package com.vhly.bpplat.twitter;

import com.google.appengine.repackaged.com.google.common.util.Base64;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Logger;

/**
 * Created by IntelliJ IDEA.
 * User: vhly[FR]
 * Date: 2010-3-22
 * Time: 13:00:50
 */
public class HTTPClient {
    private static Logger log = Logger.getLogger("HTTPClient");

    public static String request(String url, String accountInfo, byte[] reqData, boolean forcePost) {
        String xml = null;
        URL u = null;
        HttpURLConnection http;
        try {
            u = new URL(url);
            http = (HttpURLConnection) u.openConnection();
            http.setDoInput(true);
            String method = "GET";
            if (accountInfo != null && accountInfo.length() > 0) {
                http.setRequestProperty("Authorization", "Basic " + Base64.encode(accountInfo.getBytes("UTF-8")));
            }
            if (reqData != null && reqData.length > 0 || forcePost) {
                http.setDoOutput(true);
                method = "POST";
            }
            http.setRequestMethod(method);
            if (method.equals("POST")) {
                if (reqData != null) {
                    http.setRequestProperty("Content-Length", Integer.toString(reqData.length));
                    OutputStream out = http.getOutputStream();
                    out.write(reqData);
                    out.close();
                }
            }
            int responseCode = http.getResponseCode();
//            log.info("response code "+responseCode);
            if (responseCode == HttpServletResponse.SC_OK) {
                InputStream in = http.getInputStream();
                byte[] buf = readInputStream(in);
                in.close();
                if (buf != null) {
                    xml = new String(buf, "UTF-8");
                }
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
        }
        return xml;
    }

    private static byte[] readInputStream(InputStream in) {
        byte[] ret = null;
        ByteArrayOutputStream bout = null;
        byte[] buf = new byte[2048];
        int len;
        try {
            bout = new ByteArrayOutputStream();
            while (true) {
                len = in.read(buf);
                if (len == -1) {
                    break;
                }
                bout.write(buf, 0, len);
            }
            buf = null;
            ret = bout.toByteArray();
        } catch (IOException ioe) {

        } finally {
            buf = null;
            if (bout != null) {
                try {
                    bout.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return ret;
    }
}
